$(function(){
	layui.use('layer', function(){
	})
	getHistoryAddress(1);

})
function addaddresspopup(){
	/*$("#baiduaddress").val(getCookie("address"))*/
	noheardspopup1("#popupwindows","95%","250px")
}
/**
 * 添加收货地址
 * @returns
 */
function insertHistoryAddress(){
	 var province=$("select[name='province']").find("option:checked").html();
	 var city=$("select[name='city']").find("option:checked").html();
	 var area=$("select[name='area']").find("option:checked").html();
	 var town=$("select[name='town']").find("option:checked").html();
	 var address=$("#baiduaddress").val();
	 var province1=(!province ||province=="undefined"? "" : ""+province+"");
	 var city1=(!city ||city=="undefined"? "" : ""+city+"");
	 var area1=(!area ||area=="undefined"? "" : ""+area+"");
	 var town1=(!town ||town=="undefined"? "" : ""+town+"");
	/* alert($("#defaultaddress").prop("checked"))*/
	if(address==""){
		tippopup("#baiduaddress","请添加详细信息")
	}else{
		var json={"iphone":""+getCookie("useriphone")+"","address":""+province1+city1+area1+town1+address+""};
		$.ajax({
			type: "post",
			datatype: 'json',
			contentType: 'application/json;charset=UTF-8',
			url: ''+httpipstr+'alfa-mobile-ws/rest/historyaddress/insertHistoryAddress?mobiletoken='+getCookie("mobiletokenuser")+'',
			data: JSON.stringify(json),
			success: function(data) {
				if(data.message=="4"){
					tippopup("this","地址添加已达上限")
				}else{
					if($("#defaultaddress").prop("checked")){
						layer.closeAll();
						var index = layer.load(2, {
							shade: [0.1, '#fff'],
							time: 3000
						});
						getHistoryAddress(1);
					}else{
						layer.closeAll();
						getHistoryAddress(1);
					}
					
				}
			}
		})
	}
}
function onclickaddressli(id,address,data){
	$(".content").find("li").each(function(){
		if($(this).find("i").hasClass("fa-circle")){
			$(this).find("i").removeClass("fa-circle").addClass("fa-circle-o");
		}
	})
	$(data).find("i").removeClass("fa-circle-o").addClass("fa-circle");
	setcookie("recycleaddress", address, 30, "/");
	setcookie("recycleaddressid", id, 30, "/");
	layui.use('layer', function(){
		layer.load(2, {time: 1*1000})
	})
	location.href="../weixinuser/establish.jsp"
}
/**
 * 地址栏点击和长按
 * @returns
 */
var timeOutEvent=0;
function onclickaddress(){
	$(".addressli").find(".name").click(function(){
		setcookie("recycleaddress", $(this).parent().parent().attr("name"), 30, "/");
		setcookie("recycleaddressid", $(this).parent().parent().attr("id"), 30, "/");
		layui.use('layer', function(){
			layer.load(2, {time: 1*1000})
		})
		location.href="../weixinuser/establish.jsp"
	})
	/*$(".addressli").find(".name").on({
		touchstart: function(e) {
			timeOutEvent = setTimeout("longPress("+$(this).attr("id")+")", 1000);
			e.preventDefault();
		},
		touchmove: function() {
			clearTimeout(timeOutEvent);
			timeOutEvent = 0;
		},
		touchend: function() {
			clearTimeout(timeOutEvent);
			if (timeOutEvent != 0) {
				$(".content").find("li").each(function(){
					if($(this).find("i").hasClass("fa-circle")){
						$(this).find("i").removeClass("fa-circle").addClass("fa-circle-o");
					}
				})
				setcookie("recycleaddress", $(this).parent().parent().attr("name"), 30, "/");
				setcookie("recycleaddressid", $(this).parent().parent().attr("id"), 30, "/");
				layui.use('layer', function(){
					layer.load(2, {time: 1*1000})
				})
				location.href="../weixinuser/establish.jsp"
			}else{
				longPress($(this).attr("id"))
			}
			return false;
		}
	})*/
}
function longPress(id) {
	/*timeOutEvent = 0;
	$("#deladdressid").attr("name",id)
	noheardspopup1("#delpopupwindows","auto","auto")*/
	layui.use('layer', function(){ //独立版的layer无需执行这一句
		layer.confirm('确定要删除吗？', {
			btn: ['确定', '取消'] //按钮
			,yes: function(index, layero){
			delHistoryAddress(id)
			}
			,btn2: function(index, layero){
				layer.closeAll(); 
		}
		})
	})
}
function delHistoryAddress(id){
	var index = layer.load(2, {
		shade: [0.1, '#fff'],
		time: 3000
	});
	if(id==getCookie("recycleaddressid")){
		setcookie("recycleaddress", "", 30, "/");
		setcookie("recycleaddressid","", 30, "/");
	}
	var json={"id": ""+id+""}
	$.ajax({
		type: "post",
		datatype: 'json',
		contentType: 'application/json;charset=UTF-8',
		url: ''+httpipstr+'alfa-mobile-ws/rest/historyaddress/deleteHistoryAddress?mobiletoken='+getCookie("mobiletokenuser")+'',
		data: JSON.stringify(json),
		success: function(data) {
			layer.closeAll();
			getHistoryAddress(1);
		}
	})
}